package com.nari.ysphd.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nari.ysphd.R;

/*8
 * 消息弹框
 */
public class AliasNameDialog extends PopupWindow {
	private Object mObject;
	private View mMenuView;
	private Activity window;
	public LinearLayout ll_item;
	Button btn_joinhome;
	private EditText invite_code_input;
	private TextView textView1;
	private Button btn_joincancel;

	@SuppressWarnings("deprecation")
	public AliasNameDialog(final Activity context, OnClickListener itemsOnClick) {
		super(context);

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.view_alias_name_dialog, null);

		window = context;
		btn_joinhome = (Button) mMenuView.findViewById(R.id.btn_joinhome);
		setInvite_code_input((EditText)mMenuView.findViewById(R.id.code_input));
		btn_joincancel = (Button) mMenuView.findViewById(R.id.btn_joincancel);
		textView1 = (TextView) mMenuView.findViewById(R.id.textView1);
		btn_joinhome.setOnClickListener(itemsOnClick);
		// 设置按钮监听
		// 设置SelectPicPopupWindow的View
		this.setContentView(mMenuView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.MATCH_PARENT);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.MATCH_PARENT);
		// 设置SelectPicPopupWindow弹出窗体可点�?
		this.setFocusable(true);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.PopupAnimation);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(window.getResources().getColor(R.color.color_transparent_bg));
		// 设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);
		// 添加pop窗口关闭事件
		this.setOnDismissListener(new poponDismissListener());
		// mMenuView添加OnTouchListener监听判断获取触屏位置如果在�?�择框外面则�?毁弹出框
		mMenuView.setOnTouchListener(new OnTouchListener() {
			@SuppressLint("ClickableViewAccessibility")
			public boolean onTouch(View v, MotionEvent event) {

				int height = mMenuView.findViewById(R.id.pop_layout).getTop();
				int heightmin = mMenuView.findViewById(R.id.pop_layout).getBottom();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
					if (y > heightmin) {
						dismiss();
					}
				}
				return true;
			}
		});
		btn_joincancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
	}

	public Object getObject() {
		return mObject;
	}

	public void setObject(Object mObject) {
		this.mObject = mObject;
	}

	public EditText getInvite_code_input() {
		return invite_code_input;
	}

	public void setInvite_code_input(EditText invite_code_input) {
		this.invite_code_input = invite_code_input;
	}

	public void setTextView1(String title){
		this.textView1.setText(title);
	}

	/**
	 * 弹出的popWin关闭的事件，主要是为了将背景透明度改回来
	 *
	 * @author cg
	 *
	 */
	class poponDismissListener implements OnDismissListener {

		@Override
		public void onDismiss() {
			WindowManager.LayoutParams params = window.getWindow().getAttributes();
			params.alpha = 1f;
			window.getWindow().setAttributes(params);
		}

	}
}
